package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseEditActivity;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.ModifyDeviceContract;
import com.tianyang.yukuaifuzs.business.main.presenter.ModifyDevicePresenter;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：修改设备名称
 * Created by xyj on 2024/4/24
 */
public class ModifyDeviceActivity extends BaseEditActivity implements ModifyDeviceContract.View {

    @BindView(R.id.codeTv)
    TextView codeTv;
    @BindView(R.id.newNameEt)
    EditText newNameEt;

    private ModifyDeviceContract.Presenter mPresenter;

    private String userId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_modify_device;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("修改设备名称");
        String deviceName = getIntent().getStringExtra(MainConfig.INTENT_DEVICE_NAME);
        if (getIntent().hasExtra(MainConfig.INTENT_USER_ID)) {
            userId = getIntent().getStringExtra(MainConfig.INTENT_USER_ID);
        }
        codeTv.setText(VerifyUtil.checkString(deviceName));
        String deviceId = getIntent().getStringExtra(MainConfig.INTENT_DEVICE_ID);
        mPresenter = new ModifyDevicePresenter(this, deviceId, userId);
    }

    @OnClick({R.id.confirmTv})
    public void onClick(View view) {
        if (view.getId() == R.id.confirmTv) {
            String name = newNameEt.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtil.show("请输入新的设备名称");
                SoftInputUtil.openKeybord(newNameEt, mContext);
                return;
            }
            mPresenter.modifyDeviceName(name);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ModifyDeviceContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
